package tiepbm.app.store.activity;

import tiepbm.app.util.hardware.KeyboardUtil;
import tiepbm.app.util.text.ValidateUtil;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class LoginActivity extends BaseActivity implements IScreen{

	private EditText mUsernameEdt;
	private EditText mPasswordEdt;
	private String mUsername,mPassword;
	private View mErrorView;
	private Button mLoginBt;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		initView();
		implementListener();
	}

	@Override
	public void initView() {
		// TODO Auto-generated method stub
		mLoginBt = (Button) findViewById(R.id.btnLogin);
		mUsernameEdt = (EditText) findViewById(R.id.TxtLoginID);
		mPasswordEdt = (EditText) findViewById(R.id.TxtPassword);
	}

	@Override
	public void implementListener() {
		// TODO Auto-generated method stub
		mLoginBt.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				boolean cancel = validateField();
				if (cancel) {
					mErrorView.requestFocus();
					return;
				} else {
					KeyboardUtil.hideSoftKeyboard(LoginActivity.this);
				}
				processLogin();
			}
		});
	}

	@Override
	public void fillData() {
		// TODO Auto-generated method stub
		
	}
	private void processLogin(){
		
	}
	/**
	 * Check valid username and password
	 * 
	 * @return
	 */
	private boolean validateField() {
		// Reset errors
		mUsernameEdt.setError(null);
		mPasswordEdt.setError(null);

		mUsername = mUsernameEdt.getText().toString();
		mPassword = mPasswordEdt.getText().toString();

		boolean cancel = false;

		// Check valid username
		if (TextUtils.isEmpty(mUsername)) {
			mUsernameEdt
					.setError(getString(R.string.error_username_null));
			mErrorView = mUsernameEdt;
			cancel = true;
			return cancel;
		} else if (!ValidateUtil.validateUserName(mUsername)) {
			mUsernameEdt
					.setError(getString(R.string.error_username));
			mErrorView = mUsernameEdt;
			cancel = true;
			return cancel;
		}

		// Check valid password
		if (TextUtils.isEmpty(mPassword) || mPassword.length() < 6) {
			mPasswordEdt.setError(getString(R.string.error_password_length));
			mErrorView = mPasswordEdt;
			cancel = true;
			return cancel;
		}
		return cancel;
	}
}
